package com.face.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.face.server.entity.Admin;
import com.face.server.entity.Function;
import com.face.server.entity.Role;
import com.face.server.repository.AdminRepository;
import com.face.server.repository.FuncRepository;
import com.face.server.repository.RoleRepository;
import com.face.server.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by ryan on 2018/1/29.
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private AdminRepository adminRepository;

    @Autowired
    private FuncRepository funcRepository;

    @Override
    public Page<Role> listRoles(Pageable pageRequest, String searchKey) {
        if (searchKey==null)
            return roleRepository.findAll(pageRequest);
        return roleRepository.findByNameRegexOrAdminNameRegexOrFunctionListRegex(searchKey,searchKey,searchKey,pageRequest);
    }

    @Override
    public Role editAdminsInRole(String rid, String searchData) {
        Role role=roleRepository.findOne(rid);
        List<String> oriAdmins=role.getAdminId();
        List<String> adminNames=new ArrayList<String>();
        List<String> adminIds=new ArrayList<String>();
        JSONObject searchObj = new JSONObject();
        if ((searchData==null||searchData=="")&&oriAdmins!=null)
        {
            oriAdmins.forEach(e->{
                Admin admin=adminRepository.findOne(e);
                admin.setRole(null);
                adminRepository.save(admin);
            });
            role.setAdminId(null);
            role.setAdminName(null);
            return roleRepository.save(role);
        }
        JSONObject jsonObject=JSON.parseObject(searchData);
        List<String> admins=new ArrayList<String>();
        if (searchData!=null&&searchData.trim()!=null&&searchData.split(",")!=null&&jsonObject.get("searchData")!=null)
            Arrays.stream(jsonObject.get("searchData").toString().split(",")).forEach(e->admins.add(e));
//        System.out.println(searchObj);
//        JSONArray admins=searchObj.getJSONArray("admins");
        if (oriAdmins!=null){
            oriAdmins.stream().filter(e->
                    (e!=null&&admins.contains(e)==false)
            ).forEach(e->{
                Admin admin=adminRepository.findOne(e);
                admin.setRole(null);
                adminRepository.save(admin);
            });
        }
        System.out.println(admins);
        admins.stream().forEach(e->{
            Admin admin=adminRepository.findOne(e.toString());
            if (admin!=null){//如果管理员已有一个角色怎么办
                admin.setRole(role);
                adminRepository.save(admin);
                adminIds.add(admin.getId());
                adminNames.add(admin.getName());
            }
        });
        role.setAdminName(adminNames);
        role.setAdminId(adminIds);
        return roleRepository.save(role);
    }

    @Override
    public Role editFuncsInRole(String rid, String searchData) {
        Role role=roleRepository.findOne(rid);
        JSONObject searchObj = new JSONObject();
        List<Function> functions=role.getFunctions();
        List<Function> functionList=new ArrayList<Function>();
        List<String> funcNames=new ArrayList<String>();
        if (searchData==null){
            role.setFunctionList(null);
            role.setFunctions(null);
            return roleRepository.save(role);
        }
        JSONObject jsonObject=JSON.parseObject(searchData);
        List<String> funcs=new ArrayList<String>();
        if (searchData!=null&&searchData.split(",")!=null&&jsonObject.get("searchData")!=null){
            Arrays.stream(jsonObject.get("searchData").toString().split(",")).forEach(e->funcs.add(e));
        }
        funcs.stream().forEach(e->{
            Function function=funcRepository.findOne(e.toString());
            functionList.add(function);
            funcNames.add(function.getFName());
        });
        role.setFunctions(functionList);
        role.setFunctionList(funcNames);
        return roleRepository.save(role);
    }

    @Override
    public Role createRole(String name) {
        Role role=new Role();
        role.setName(name);
        return roleRepository.save(role);
    }

    @Override
    public Role listMyRole() {
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin) subject.getPrincipal();
        String adminId=admin.getId();
        return roleRepository.findByAdminId(adminId);
    }
}
